import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { brandList } from '#/api/trade/brand';
import { skuList } from '#/api/trade/sku';
import { spuList } from '#/api/trade/spu';

import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  
  {
    component: 'Input',
    fieldName: 'orderNo',
    label: '订单编号',
  },
  {
    component: 'Input',
    fieldName: 'saleContractNo',
    label: '销售合同',
  },
  {
    component: 'Input',
    fieldName: 'purchaseContractNo',
    label: '采购合同',
  },
  {
    component: 'Input',
    fieldName: 'customerName',
    label: '客户名称',
  },
  {
    component: 'Input',
    fieldName: 'supplierName',
    label: '供应商',
  },
  
  {
    component: 'Select',
    componentProps: {
      // 可选从DictEnum中获取 DictEnum.ORDER_STATUS 便于维护
      options: getDictOptions('order_status'),
    },
    fieldName: 'status',
    label: '合同状态',
  },
  {
    component: 'Input',
    fieldName: 'frameNo',
    label: '车架号',
  },
  {
    component: 'Input',
    fieldName: 'logisticsPic',
    label: '物流信息',
  },
  // {
  //   component: 'ProductCascader',
  //   fieldName: 'productInfo',
  //   label: '产品信息',
  //   formItemClass: 'col-span-2',  // 增加组件宽度，占用2列
  //   componentProps: {
  //     showSku: true,     
  //     useFormItem: false,
  //     placeholder: {
  //       brand: '请选择品牌',
  //       spu: '请选择SPU'
  //     },
  //   },
  // },
  {
    component: 'ApiSelect',
    componentProps: {
      api: () => brandList(),
      resultField: 'rows',
      labelField: 'brandName',
      valueField: 'id',
      showSearch: true,
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().includes(input.toLowerCase());
      },
    },
    label: '品牌',
    fieldName: 'brandId',
  },
  {
    component: 'ApiSelect',
    componentProps: (formModel) => {
      return {
        api: () => spuList({ brandId: formModel.brandId }),
        resultField: 'rows',
        labelField: 'spuName',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
        placeholder: '请选择SPU' ,
      };
    },
    label: 'SPU',
    fieldName: 'spuId',
  },
  {
    component: 'ApiSelect',
    componentProps: (formModel) => {
      return {
        api: () => skuList({ brandId: formModel.brandId }),
        resultField: 'rows',
        labelField: 'skuName',
        valueField: 'id',
        showSearch: true,
        filterOption: (input: string, option: any) => {
          return option.label.toLowerCase().includes(input.toLowerCase());
        },
        placeholder: '请选择SKU' ,
      };
    },
    label: 'SKU',
    fieldName: 'skuId',
  },
];

// 基础列配置
const baseColumns: VxeGridProps['columns'] = [
  { type: 'checkbox', width: 60 },
  {
    title: '订单编号',
    field: 'orderNo',
    slots: {
      default: 'orderNo'
    },
    width: 170,
  },
  {
    title: '销售合同',
    field: 'saleContractNo',
    width: 170,
  },
  {
    title: '采购合同',
    field: 'purchaseContractNo',
    width: 170,
  },
  {
    title: '客户名称',
    field: 'customerName',
    width: 160,
  },
  {
    title: '品牌',
    field: 'brandName',
  },
  {
    title: 'SPU',
    field: 'spuName',
  },
  {
    title: 'SKU',
    field: 'skuName',
  },
  {
    title: '合同状态',
    field: 'status',
    width: 100,
    slots: {
      default: ({ row }) => {
        // 可选从DictEnum中获取 DictEnum.ORDER_STATUS 便于维护
        return renderDict(row.status, 'order_status');
      },
    },
  },
  {
    title: '车架号',
    field: 'frameNo',
    width: 100,
  },
  {
    title: '销售',
    field: 'saleUserName',
    width: 100,
  },
  {
    title: '生成时间',
    field: 'createTime',
    width: 180,
  },
];

// 根据状态动态生成列配置
export const getColumns = (status?: string): VxeGridProps['columns'] => {
  const columns = [...baseColumns];
  
  // 可以根据不同状态添加或移除特定列
  // 例如：已作废状态可能需要显示作废原因等
  
  return columns;
};

// 默认列配置（保持向后兼容）
export const columns: VxeGridProps['columns'] = getColumns();

